package copy.express.process.spit.impl;


import copy.express.pojo.Node;
import copy.express.pojo.type.Type;
import copy.express.process.spit.ConfirmTypeEndIndex;
import copy.tools.StringStrTools;

public class SymbolTypeEndIndex implements ConfirmTypeEndIndex {

    private static SymbolTypeEndIndex symbolTypeEndIndex = new SymbolTypeEndIndex();

    private SymbolTypeEndIndex(){

    }

    public static SymbolTypeEndIndex getInstance(){
        return symbolTypeEndIndex;
    }

    /**
     *
     * @param c
     * @return 拥有含义,表示字符的长度
     */
    @Override
    public boolean confirmType(char c) {
        String t = ""+c;
        //遍历字符串枚举
        for (Type.SymbolEnum sym : Type.SymbolEnum.values()) {
            if (sym.value.contains(t)){
                return true;
            }
        }
        return false;
    }

    @Override
    public int findEndIndex(int start,int pos, char[] chars) {
        if (pos==chars.length){
            return pos;
        }
        if ( chars[pos] == ' ' || chars[pos] == ','  )return pos;

        //检查前一个符号: 有写符号的长度是1,不需要拼接
        if (start-1>0){
            String c = ""+chars[start-1];
            if (c.equals("(") || c.equals(")")){
                return start;
            }
        }
        //上面操作是一个短路操作
        if ( confirmType(chars[pos])  ){
            //连续存在的符号
            if (chars[pos] == '=' || chars[pos] == chars[pos-1]){

                return findEndIndex(start,pos+1,chars);
            }
            //这个符号 和 上一个符号,不相等.停止查找
            return pos;
        }else{
            //字符串subString位置: start-1,pos
            return pos;
        }

    }

    @Override
    public Node createNode(int start, int end, char[] chars) {
        String result = StringStrTools.appendFromStartToEnd(start,end-1,chars);
        Type.SymbolEnum symbolEnum = null;
        for (Type.SymbolEnum e : Type.SymbolEnum.values()) {
            if (e.value.equals(result)){
                symbolEnum = e;
            }
        }
        return new Node(symbolEnum,symbolEnum);
    }
}
